package com.sc.smartcommunity.controller;

import cn.dev33.satoken.annotation.SaCheckLogin;
import com.sc.smartcommunity.service.PaymentRecordService;
import com.sc.smartcommunity.service.PaymentService;
import com.sc.smartcommunity.domain.vo.PaymentVO;
import com.sc.smartcommunity.domain.vo.ResultVO;
import jakarta.annotation.Resource;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.math.BigDecimal;

@RestController
@RequestMapping("/api/payment")
public class PaymentController {

    @Resource
    private PaymentService paymentService;


    /**
     * 创建缴费记录
     */
    @PostMapping("/create")
    @SaCheckLogin
    public ResultVO createPayment(@RequestBody PaymentVO paymentVO) {
        return paymentService.createPayment(paymentVO);
    }

    /**
     * 获取用户缴费记录
     */
    @GetMapping("/user/{userId}")
    @SaCheckLogin
    public ResultVO getUserPayments(@PathVariable Long userId,
                                    @RequestParam(defaultValue = "1") Integer page,
                                    @RequestParam(defaultValue = "10") Integer size) {
        return paymentService.getUserPayments(userId, page, size);
    }

    /**
     * 支付订单
     */
    @PostMapping("/pay/{paymentId}")
    @SaCheckLogin
    public ResultVO payOrder(@PathVariable Long paymentId,
                             @RequestParam Integer payChannel) {
        return paymentService.payOrder(paymentId, payChannel);
    }

    /**
     * 获取缴费详情
     */
    @GetMapping("/detail/{paymentId}")
    @SaCheckLogin
    public ResultVO getPaymentDetail(@PathVariable Long paymentId) {
        return paymentService.getPaymentDetail(paymentId);
    }

    @Resource
    private PaymentRecordService paymentRecordService;

    // 添加@SaCheckLogin注解以启用登录验证
    @GetMapping("/total/success")
    @SaCheckLogin
    public ResponseEntity<BigDecimal> getTotalSuccessPaymentAmount() {
        BigDecimal totalAmount = paymentRecordService.getTotalSuccessPaymentAmount();
        return ResponseEntity.ok(totalAmount);
    }
}
